package cn.study.controller;


import cn.study.entity.*;
import cn.study.service.StudyEmpResultService;
import cn.study.service.StudyExaminationService;
import cn.study.service.StudyTestPaperParticularsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * <p>
 * 考试表 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Slf4j
@Controller
@RequestMapping("/study-examination")
public class StudyExaminationController {
    @Autowired
    StudyTestPaperParticularsService studyTestPaperParticularsService;

    @Autowired
    StudyEmpResultService studyEmpResultService;
    @Autowired
    StudyExaminationService studyExaminationService;

    @GetMapping("/k1/{depId}")
    public String k1(Model model,
                     @PathVariable("depId") Integer depId,
                     @RequestParam(value="pn",defaultValue="1") Integer pn,
                     @RequestParam(value="paginalsize",defaultValue = "3") Integer paginalsize)
    {
        /*查询员工部门的考试*/

        PageHelper.startPage(pn, paginalsize);
        List<StudyExamination> studyExaminationlist=studyExaminationService.studyExaminlist(depId);
        PageInfo pInfo=new PageInfo(studyExaminationlist,10);
        model.addAttribute("pInfo",pInfo);
        return  "exam";
    }



    @GetMapping("/kao")
    public String kao(@RequestParam(value = "testPaperId",defaultValue = "1") Integer testPaperId,
                      Model model,Integer examinationPaperduration) {
        QueryWrapper<StudyTestPaperParticulars> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("test_paper_id",testPaperId);
        List<StudyTestPaperParticulars> testPaperParticulars=studyTestPaperParticularsService.list(queryWrapper);
        model.addAttribute("list",testPaperParticulars);
        StudyExamination studyExamination=new StudyExamination();
        studyExamination.setExaminationPaperduration(examinationPaperduration);
        model.addAttribute("list111",studyExamination);
        return "kao";
    }

    @PostMapping("handpaper")
    public String  handpaper(StudyEmpResultList list , Model model ){
        /*获取准确答案===========================*/
        QueryWrapper<StudyTestPaperParticulars> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("test_paper_id",1);
        queryWrapper.orderByAsc("test_paper_num");
        //准确答案
        List<StudyTestPaperParticulars> testPaperParticulars=studyTestPaperParticularsService.list();
        List<StudyEmpResult> submitlist=list.getList();
        /*=================================================*/
        System.out.println(list);
        Integer score=0;
        for (int i=0;i<submitlist.size();i++) {
            log.info(submitlist.get(i).getEmpResultAnswer());
            /*判断题目类型*/
            if(submitlist.get(i).getEmpResultTestType()==1)
            {
                log.info("==========="+testPaperParticulars.get(i).getTestPaperScore());
                if(submitlist.get(i).getEmpResultAnswer().equals(testPaperParticulars.get(i).getOptionOrAnswer())){
                    score=score+testPaperParticulars.get(i).getTestPaperScore();
                }
            }else if(submitlist.get(i).getEmpResultTestType()==2){
                boolean bl =true;
                String[] submitanw=submitlist.get(i).getEmpResultAnswer().split(",");
                String[] exactanw=testPaperParticulars.get(i).getOptionOrAnswer().split(",");
               if(submitanw.length==exactanw.length){
                   duopan:for(int j=0;j<submitanw.length;j++){
                      if(!(submitanw[j].equals(exactanw[j]))){
                          bl=false;
                          break duopan;
                      }
                   }
               }else {
                   bl=false;
               }
               if(bl){
                   score=score+testPaperParticulars.get(i).getTestPaperScore();
               }
            }else {
                break;
            }
        }
        boolean a=  studyEmpResultService.saveBatch(submitlist);

        model.addAttribute("list",testPaperParticulars);

        model.addAttribute("list2",submitlist);

        StudyTestPaperParticulars studyTestPaperParticulars=new StudyTestPaperParticulars();
        studyTestPaperParticulars.setTestPaperScore(score);
        model.addAttribute("score",studyTestPaperParticulars);
        return "kao3";
    }
    @ResponseBody
    @PostMapping("/judgmentexam")
    public Map<String,Object> judgmentexam(@RequestParam Integer id){
        System.out.println(id);
       StudyExamination studyExamination= studyExaminationService.getById(id);
        long minutes = ChronoUnit.MINUTES.between(Instant.ofEpochMilli(new Date().getTime()), Instant.ofEpochMilli(studyExamination.getExaminationEndTime().getTime()));
        int time= (int) minutes;
        boolean effectiveDate = isEffectiveDate(new Date(), studyExamination.getExaminationBeginTime(), studyExamination.getExaminationEndTime());
       Map<String,Object> map= new HashMap<>();
       map.put("bl",effectiveDate);
      map.put("testPaperId",studyExamination.getTestPaperId());
      map.put("examinationPaperduration",time);
        return map;
    }
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
}

